package collection;

import java.util.*;

public class CollectionTestDemo {
    public static void main(String[] args) {
        //
        //创建一个集合对象
        Collection<String> c=new ArrayList<>();
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        System.out.println(c);
        //Collection---->Array
        String[] s=c.toArray(new String[c.size()]);
        //遍历数组
        for (String s1:s){
            System.out.println(s1);
        }
        //Array------->Collection
        //asList静态方法需要Arrays.来调用
        List<String> list= Arrays.asList(s);
        System.out.println(list);

        //Collections集合工具类的使用
        //sort 比较 按照ASCII码排序
        Collections.sort(list);
        System.out.println("list排序后："+list);
        //reverse 反转
        Collections.reverse(list);
        System.out.println("list反转后："+list);
        //集合中二点元素会被改变
        //比较自定义的类型
        Collections.sort(list,(x,y)->{
            return x.length()-y.length();
        });
        System.out.println("list排序后："+list);
        List<Point> list1=new ArrayList<>();
        list1.add(new Point(5,8));
        list1.add(new Point(33,290));
        list1.add(new Point(21,52));
        list1.add(new Point(45,6));
        list1.add(new Point(55,69));
        System.out.println("list1原始数据:"+list1);
        Collections.sort(list1,(o1,o2)->{
            int line1= o1.getX()*o1.getX()+o1.getY()*o1.getY();
            int line2= o2.getX()*o2.getX()+o2.getY()*o2.getY();
            return line1-line2;
        });
        //自定义比较器,o1-o2是升序 o2-o1是降序
        System.out.println("list1排序后:"+list1);

    }
}
